/**
 * 定义全局过滤器
 *
 * 时间戳转换日期
 *
 */

let globalFilter = {
  // 时间戳转换日期
  formatDate: (value) => {
    let date = new Date(value)
    let y = date.getFullYear()
    let MM = date.getMonth() + 1
    MM = MM < 10 ? '0' + MM : MM
    let d = date.getDate()
    d = d < 10 ? '0' + d : d
    let h = date.getHours()
    h = h < 10 ? '0' + h : h
    let m = date.getMinutes()
    m = m < 10 ? '0' + m : m
    let s = date.getSeconds()
    s = s < 10 ? '0' + s : s
    return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
  },
}

/**
 * 时间格式化
 * type: 格式化类型
 */
export const timeFormat = (time, type = "") => {
  const date = new Date(time);
  const y = date.getFullYear();
  let m = date.getMonth();
  let d = date.getDate();
  let hh = date.getHours();
  let mm = date.getMinutes();
  let ss = date.getSeconds();
  m = m < 10 ? "0" + m : m;
  d = d < 10 ? "0" + d : d;
  hh = hh < 10 ? "0" + hh : hh;
  mm = mm < 10 ? "0" + mm : mm;
  ss = ss < 10 ? "0" + ss : ss;
  let result = "";
  type = type.toLocaleLowerCase();
  switch (type) {
    case "yyyy-mm-dd":
      result = `${y}-${m}-${d}`;
      break;
    case "mm-dd":
      result = `${m}-${d}`;
      break;
    case "hh-mm":
      result = `${hh}:${mm}`;
      break;
    default:
      result = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
      break;
  }
  return result;
};

// 字符串转日期格式，strDate要转为日期格式的字符串
export const getStringToDate = (strDate) => {
  var st = strDate;
  var a = st.split(" ");
  var b = a[0].split("-");
  var c = a[1].split(":");
  var date = new Date(b[0], b[1], b[2], c[0], c[1])
  return date;
}

export default globalFilter
